package com.lanou.myim.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.lanou.myim.R;
import com.lanou.myim.tools.ParseSingleton;

import java.io.File;

/**
 * Created by dllo on 16/3/10.
 */
public class PersonalDataActivity extends BaseActivity implements View.OnClickListener{
    private ImageView personalHead;
    private TextView personalName;
    private TextView personalNickname;
    private TextView persoalindividuality;
    public final static int CONSULT_DOC_PICTURE = 200;
    public final static int CONSULT_DOC_CAMERA = 201;
    private String userName;
    private int SELECT_PICTURE = 0;
    private int SELECT_CAMERA = 1;
    private Bitmap bmp;
    private Uri outputFileUri;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personaldata);
        persoalindividuality = (TextView) findViewById(R.id.personal_individuality);
        personalHead = (ImageView) findViewById(R.id.personal_head);
        personalName = (TextView) findViewById(R.id.personal_name);
        personalNickname = (TextView) findViewById(R.id.personal_nickname);
        personalHead.setOnClickListener(this);
        personalNickname.setOnClickListener(this);
        userName = EMChatManager.getInstance().getCurrentUser();
        personalName.setText(userName);
       ParseSingleton.setDescr(userName, personalNickname);
        ParseSingleton.setBitmap(userName, personalHead);
        ParseSingleton.setDescr(userName, personalNickname);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.personal_head:
                cameraClickListener.onClick(view);
              break;
            case R.id.personal_individuality:
                break;
            case R.id.personal_name:
                break;
            case R.id.personal_nickname:
                LayoutInflater factory = LayoutInflater.from(this);
                final View view1 = factory.inflate(R.layout.editbox, null);
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("设置昵称").setView(view1).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ParseSingleton.insertOrModifyDescr(userName,editText.getText().toString());
                        personalNickname.setText(editText.getText().toString());
                    }
                }).setNegativeButton("取消", null).create().show();
                break;
        }


    }
    private View.OnClickListener cameraClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            CharSequence[] items = { "相册", "相机" };
            new AlertDialog.Builder(PersonalDataActivity.this).setTitle("选择图片来源").setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == SELECT_PICTURE) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
                        intent.setType("image/*");
                        intent.putExtra("crop", "true");
                        intent.putExtra("aspectX", 1);// 裁剪框比例
                        intent.putExtra("aspectY", 1);
                        intent.putExtra("outputX", 150);// 输出图片大小
                        intent.putExtra("outputY", 150);
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, CONSULT_DOC_PICTURE);
                    } else {

                        File file = new File(Environment.getExternalStorageDirectory(), "textphoto.jpg");
                        outputFileUri = Uri.fromFile(file);
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                        startActivityForResult(intent, CONSULT_DOC_CAMERA);
                    }
                }
            }).create().show();

        }
    };

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CONSULT_DOC_PICTURE) {
            if(data == null){
                return;
            }

            if (bmp != null)// 如果不释放的话，不断取图片，将会内存不够
                bmp.recycle();
            bmp = data.getParcelableExtra("data");
            if (bmp != null){
                ParseSingleton.insertOrModifyPersonPicture(userName,bmp);
            }

            //bmp = BitmapFactory.decodeFile(path);
            personalHead.setImageBitmap(bmp);

        } else if (requestCode == CONSULT_DOC_CAMERA) {
            if(data==null){
                return;
            }
            bmp = (Bitmap) data.getExtras().get("data");
           // bmp = BitmapFactory.decodeFile(outputFileUri.getPath());
            //bmp = data.getParcelableExtra("data");

            if (bmp != null){
                ParseSingleton.insertOrModifyPersonPicture(userName,bmp);
            }

            personalHead.setImageBitmap(bmp);
        } else {
            Toast.makeText(this, "请重新选择图片", Toast.LENGTH_SHORT).show();
        }

    }
}
